package com.yby6.led.test;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yby6.led.utils.SetSystemTime;

import java.util.Calendar;

public class Init {

    public static void main(String[] args) throws Exception {
        // 码云LED开始时间
        DateTime startDate = DateUtil.parseDate("2024/05/09");
        String startTime = "00:43:00";

        // 定义LED数组
        int[][] led = {
            {8, 0, 0, 0, 24, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 24, 24, 24, 24, 24, 24, 0, 0, 0, 24, 0, 0, 24, 0, 0, 0, 0, 0},
            {0, 0, 0, 24, 1, 24, 24, 24, 24, 1, 24, 0, 0, 0, 24, 16, 8, 8, 1, 8, 8, 16, 24, 0, 0, 24, 8, 8, 1, 1, 1, 8, 8, 24, 0, 0, 24, 16, 16, 16, 16, 24, 0, 0, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0},
            {0, 0, 0, 24, 1, 1, 1, 1, 1, 1, 24, 0, 0, 0, 24, 8, 8, 1, 1, 1, 8, 8, 24, 0, 24, 16, 8, 8, 1, 1, 1, 8, 8, 16, 24, 0, 24, 1, 16, 16, 1, 24, 0, 0, 24, 1, 16, 16, 1, 24, 0, 0, 0, 0},
            {0, 8, 8, 24, 1, 24, 1, 1, 24, 1, 24, 8, 8, 0, 24, 16, 24, 24, 24, 24, 24, 16, 24, 0, 24, 16, 16, 24, 24, 24, 24, 24, 16, 16, 24, 0, 24, 16, 16, 16, 16, 24, 0, 0, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0},
            {0, 0, 0, 24, 1, 1, 16, 16, 1, 1, 24, 0, 0, 0, 24, 24, 1, 24, 1, 24, 1, 24, 24, 0, 0, 24, 24, 1, 24, 1, 24, 1, 24, 24, 0, 0, 24, 24, 24, 24, 24, 24, 0, 24, 0, 0, 24, 24, 0, 0, 0, 0, 0, 0},
            {0, 8, 8, 24, 1, 1, 1, 1, 1, 1, 24, 8, 8, 0, 0, 24, 1, 1, 1, 1, 1, 24, 0, 0, 0, 0, 24, 1, 1, 1, 1, 1, 24, 0, 0, 0, 0, 0, 24, 24, 0, 0, 0, 0, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 24, 24, 24, 0, 0, 0, 0, 0}
        };


//		int [] [] Led= {
//				{0,0,0,0,8,8,8,0,0,8,8,8,0,0,0,8,8,8,8,8,8,0,0,0,0,0,0,0,0,8,8,0,0,0,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,8,8,8,8,8,8,0,0,0,0,8,0,0,0,0,8,0,0,0,0,0,0,0,0,8,8,8,0,8,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,0,8,8,8,8,0,0,0,0,0,8,0,0,0,8,8,0,0,0,0,0,0,0,0,0,8,8,8,8,8,8,0,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,0,0,0,8,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
//		};

//		int [] [] Led= {
//				{0,0,0,0,8,8,8,0,0,8,8,8,0,0,0,8,8,8,8,8,8,0,0,0,0,0,0,0,0,8,8,0,0,0,8,8,0,0,0,0,0,24,24,0,0,24,24,24,24,24,0,0,24,24},
//				{0,0,0,0,0,8,8,8,8,8,8,0,0,0,0,8,0,0,0,0,8,0,0,0,0,0,0,0,0,8,8,8,0,8,8,8,0,0,0,0,0,24,24,0,0,24,24,24,24,24,0,0,24,24},
//				{0,0,0,0,0,0,8,8,8,8,0,0,0,0,0,8,0,0,0,8,8,0,0,0,0,0,0,0,0,0,8,8,8,8,8,8,0,24,0,0,0,24,24,0,0,24,24,24,24,24,0,0,24,24},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,24,24,0,24,24,24,24,24,24,24,0,24,24},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,0,0,0,8,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,24,24,0,0,24,24,24,24,24,0,0,24,24},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,24,24,0,0,24,24,24,24,24,24,0,24,24},
//				{0,0,0,0,0,0,0,8,8,0,0,0,0,0,0,8,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,8,8,8,0,0,0,8,0,0,0,24,24,0,24,24,0,0,0,24,24,0,24,24}
//		};
        
        // 设置系统时间
        boolean setTimeSuccess = SetSystemTime.setDateTimeBat(startTime, startDate.toDateStr());
        System.out.println("设置系统时间> " + setTimeSuccess);
        Thread.sleep(500L);

        // 开始处理LED数组
        processLedArray(led, startDate);
        System.out.println(">> 完成");
    }

    private static void processLedArray(int[][] led, DateTime startDate) throws InterruptedException {
        int dayNum = 0;
        for (int col = 0; col < led[0].length; col++) {
            for (int[] ints : led) {
                int submitCount = ints[col];
                int executedCount = commitMultipleTimes(submitCount);
                System.out.print(DateUtil.now() + ">> 执行第 " + executedCount + " 次");
                
                // 设置下一个日期
                dayNum++;
                String nextDate = incrementDateByDays(startDate, dayNum);
                String currentTime = DateUtil.formatTime(DateUtil.date());
                boolean nextTimeSet = SetSystemTime.setDateTimeBat(currentTime, nextDate);
                System.out.println("下一个时间: " + nextDate + " 设置成功: " + nextTimeSet);
                System.out.println();
            }
        }
    }

    private static int commitMultipleTimes(int times) throws InterruptedException {
        int executedCount = 0;
        for (int i = 0; i < times; i++) {
            boolean commitSuccess = GitUtil.commitFiles();
            while (!commitSuccess) {
                Thread.sleep(500L);
                commitSuccess = true;
                System.err.println("等待提交");
            }
            executedCount++;
        }
        return executedCount;
    }

    private static String incrementDateByDays(DateTime startDate, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE, days);
        return DateUtil.format(calendar.getTime(), "yyyy/MM/dd");
    }
}
